home *** CD-ROM | disk | FTP | other *** search
/ Windows News 2005 November / WNnov2005.iso / Windows / Equipement / hMailServer / hMailServer-4.1-Build-136.exe / {app} / PHPWebAdmin / include / minixml / classes / node.inc.php < prev    next >
Encoding:
PHP Script  |  2004-10-25  |  7.9 KB  |  322 lines

  1. <?php
  2.  
  3.  
  4. /***************************************************************************************************
  5. ****************************************************************************************************
  6. *****
  7. *****      MiniXML - PHP class library for generating and parsing XML.
  8. *****
  9. *****      Copyright (C) 2002,2003 Patrick Deegan, Psychogenic.com
  10. *****      All rights reserved.
  11. *****
  12. *****      http://minixml.psychogenic.com
  13. *****
  14. *****   This program is free software; you can redistribute
  15. *****   it and/or modify it under the terms of the GNU
  16. *****   General Public License as published by the Free
  17. *****   Software Foundation; either version 2 of the
  18. *****   License, or (at your option) any later version.
  19. *****
  20. *****   This program is distributed in the hope that it will
  21. *****   be useful, but WITHOUT ANY WARRANTY; without even
  22. *****   the implied warranty of MERCHANTABILITY or FITNESS
  23. *****   FOR A PARTICULAR PURPOSE.  See the GNU General
  24. *****   Public License for more details.
  25. *****
  26. *****   You should have received a copy of the GNU General
  27. *****   Public License along with this program; if not,
  28. *****   write to the Free Software Foundation, Inc., 675
  29. *****   Mass Ave, Cambridge, MA 02139, USA.
  30. *****
  31. *****
  32. *****   You may contact the author, Pat Deegan, through the
  33. *****   contact section at http://www.psychogenic.com
  34. *****
  35. *****   Much more information on using this API can be found on the
  36. *****   official MiniXML website - http://minixml.psychogenic.com
  37. *****    or within the Perl version (XML::Mini) available through CPAN
  38. *****
  39. ****************************************************************************************************
  40. ***************************************************************************************************/
  41.  
  42.  
  43.  
  44.  
  45. require_once(MINIXML_CLASSDIR . "/treecomp.inc.php");
  46.  
  47. /***************************************************************************************************
  48. ****************************************************************************************************
  49. *****
  50. *****                      MiniXMLNode
  51. *****
  52. ****************************************************************************************************
  53. ***************************************************************************************************/
  54.  
  55.  
  56. /* class MiniXMLNode
  57. ** MiniXMLNodes are used as atomic containers for numerical and text data
  58. ** and act as leaves in the XML tree.
  59. **
  60. ** They have no name or children.
  61. **
  62. ** They always exist as children of MiniXMLElements.
  63. ** For example,
  64. ** <B>this text is bold</B>
  65. ** Would be represented as a MiniXMLElement named 'B' with a single
  66. ** child, a MiniXMLNode object which contains the string 'this text
  67. ** is bold'.
  68. **
  69. ** a MiniXMLNode has
  70. ** - a parent
  71. ** - data (text OR numeric)
  72. */
  73.  
  74. class MiniXMLNode extends MiniXMLTreeComponent {
  75.  
  76.  
  77.     var $xtext;
  78.     var $xnumeric;
  79.  
  80.     /* MiniXMLNode [CONTENTS]
  81.     ** Constructor.  Creates a new MiniXMLNode object.
  82.     **
  83.     */
  84.     function MiniXMLNode ($value=NULL, $escapeEntities=NULL)
  85.     {
  86.         $this->MiniXMLTreeComponent();
  87.         $this->xtext = NULL;
  88.         $this->xnumeric = NULL;
  89.  
  90.         /* If we were passed a value, save it as the
  91.         ** appropriate type
  92.         */
  93.         if (! is_null($value))
  94.         {
  95.             if (is_numeric($value))
  96.             {
  97.                 if (MINIXML_DEBUG > 0)
  98.                 {
  99.                     _MiniXMLLog("Setting numeric value of node to '$value'");
  100.                 }
  101.  
  102.                 $this->xnumeric = $value;
  103.             } else {
  104.                 if (MINIXML_IGNOREWHITESPACES > 0)
  105.                 {
  106.                     $value = trim($value);
  107.                     $value = rtrim($value);
  108.                 }
  109.  
  110.                 if (! is_null($escapeEntities))
  111.                 {
  112.                     if ($escapeEntities)
  113.                     {
  114.                         $value = htmlentities($value);
  115.                     }
  116.                 } elseif (MINIXML_AUTOESCAPE_ENTITIES > 0) {
  117.                     $value = htmlentities($value);
  118.                 }
  119.  
  120.                 if (MINIXML_DEBUG > 0)
  121.                 {
  122.                     _MiniXMLLog("Setting text value of node to '$value'");
  123.                 }
  124.  
  125.                 $this->xtext = $value;
  126.  
  127.  
  128.             } /* end if value numeric */
  129.  
  130.         } /* end if value passed */
  131.  
  132.     } /* end MiniXMLNode constructor */
  133.  
  134.     /* getValue
  135.     **
  136.     ** Returns the text or numeric value of this Node.
  137.     */
  138.     function getValue ()
  139.     {
  140.         $retStr = NULL;
  141.         if (! is_null($this->xtext) )
  142.         {
  143.             $retStr = $this->xtext;
  144.         } elseif (! is_null($this->xnumeric))
  145.         {
  146.             $retStr = "$this->xnumeric";
  147.         }
  148.  
  149.         if (MINIXML_DEBUG > 0)
  150.         {
  151.             _MiniXMLLog("MiniXMLNode::getValue returning '$retStr'");
  152.         }
  153.  
  154.         return $retStr;
  155.     }
  156.  
  157.  
  158.     /* text [SETTO [SETTOALT]]
  159.     **
  160.     ** The text() method is used to get or set text data for this node.
  161.     **
  162.     ** If SETTO is passed, the node's content is set to the SETTO string.
  163.     **
  164.     ** If the optional SETTOALT is passed and SETTO is false, the
  165.     ** node's value is set to SETTOALT.
  166.     **
  167.     ** Returns this node's text, if set or NULL
  168.     **
  169.     */
  170.     function text ($setToPrimary = NULL, $setToAlternate=NULL)
  171.     {
  172.         $setTo = ($setToPrimary ? $setToPrimary : $setToAlternate);
  173.  
  174.         if (! is_null($setTo))
  175.         {
  176.             if (! is_null($this->xnumeric) )
  177.             {
  178.                 return _MiniXMLError("MiniXMLNode::text() Can't set text for element with numeric set.");
  179.  
  180.             } elseif (! is_string($setTo) && ! is_numeric($setTo) ) {
  181.  
  182.                 return _MiniXMLError("MiniXMLNode::text() Must pass a STRING value to set text for element ('$setTo').");
  183.             }
  184.  
  185.             if (MINIXML_IGNOREWHITESPACES > 0)
  186.             {
  187.                 $setTo = trim($setTo);
  188.                 $setTo = rtrim($setTo);
  189.             }
  190.  
  191.  
  192.             if (MINIXML_AUTOESCAPE_ENTITIES > 0)
  193.             {
  194.                 $setTo = htmlentities($setTo);
  195.             }
  196.  
  197.  
  198.             if (MINIXML_DEBUG > 0)
  199.             {
  200.                 _MiniXMLLog("Setting text value of node to '$setTo'");
  201.             }
  202.  
  203.             $this->xtext = $setTo;
  204.  
  205.         }
  206.  
  207.         return $this->xtext;
  208.     }
  209.  
  210.     /* numeric [SETTO [SETTOALT]]
  211.     **
  212.     ** The numeric() method is used to get or set numerical data for this node.
  213.     **
  214.     ** If SETTO is passed, the node's content is set to the SETTO string.
  215.     **
  216.     ** If the optional SETTOALT is passed and SETTO is NULL, the
  217.     ** node's value is set to SETTOALT.
  218.     **
  219.     ** Returns this node's text, if set or NULL
  220.     **
  221.     */
  222.     function numeric ($setToPrim = NULL, $setToAlt = NULL)
  223.     {
  224.         $setTo = is_null($setToPrim) ? $setToAlt : $setToPrim;
  225.  
  226.         if (! is_null($setTo))
  227.         {
  228.             if (! is_null($this->xtext)) {
  229.  
  230.                 return _MiniXMLError("MiniXMLElement::numeric() Can't set numeric for element with text.");
  231.  
  232.             } elseif (! is_numeric($setTo))
  233.             {
  234.                 return _MiniXMLError("MiniXMLElement::numeric() Must pass a NUMERIC value to set numeric for element.");
  235.             }
  236.  
  237.             if (MINIXML_DEBUG > 0)
  238.             {
  239.                 _MiniXMLLog("Setting numeric value of node to '$setTo'");
  240.             }
  241.             $this->xnumeric = trim($setTo);
  242.         }
  243.  
  244.         return $this->xnumeric;
  245.     }
  246.  
  247.  
  248.  
  249.     /* toString [DEPTH]
  250.     **
  251.     ** Returns this node's contents as a string.
  252.     **
  253.     **
  254.     ** Note: Nodes have only a single value, no children.  It is
  255.     ** therefore pointless to use the same toString() method split as
  256.     ** in the MiniXMLElement class.
  257.     **
  258.     */
  259.  
  260.     function toString ($depth=0)
  261.     {
  262.         if ($depth == MINIXML_NOWHITESPACES)
  263.         {
  264.             return $this->toStringNoWhiteSpaces();
  265.         }
  266.  
  267.         if (MINIXML_DEBUG > 0)
  268.         {
  269.             _MiniXMLLog("MiniXMLNode::toString() call with depth $depth");
  270.         }
  271.  
  272.         $spaces = $this->_spaceStr($depth);
  273.         $retStr = $spaces;
  274.  
  275.         if (! is_null($this->xtext) )
  276.         {
  277.             /* a text element */
  278.             $retStr .= $this->xtext;
  279.         } elseif (! is_null($this->xnumeric)) {
  280.             /* a numeric element */
  281.             $retStr .=  $this->xnumeric;
  282.         }
  283.  
  284.         /* indent all parts of the string correctly */
  285.         $retStr = preg_replace("/\n\s*/sm", "\n$spaces", $retStr);
  286.  
  287.         return trim($retStr);
  288.     }
  289.  
  290.  
  291.     function toStringWithWhiteSpaces ($depth=0)
  292.     {
  293.         return $this->toString($depth);
  294.     }
  295.  
  296.     function toStringNoWhiteSpaces ()
  297.     {
  298.  
  299.         if (MINIXML_DEBUG > 0)
  300.         {
  301.             _MiniXMLLog("MiniXMLNode::toStringNoWhiteSpaces() call with depth $depth");
  302.         }
  303.  
  304.         if (! is_null($this->xtext) )
  305.         {
  306.             /* a text element */
  307.             $retStr = $this->xtext;
  308.         } elseif (! is_null($this->xnumeric)) {
  309.             /* a numeric element */
  310.             $retStr =  $this->xnumeric;
  311.         }
  312.  
  313.         return trim($retStr);
  314.     }
  315.  
  316.  
  317. } /* end class definition */
  318.  
  319.  
  320.  
  321. ?>
  322.